Xrm = window.Xrm || { __namespace: true };
Xrm.Undo = Xrm.Undo || { __namespace: true };
Xrm.Undo.OnSaveAdded = false;

$(function () {
    var formType = window.parent.Xrm.Page.ui.getFormType();
    if (formType === 1) {
        Xrm.UndoShared.DisplayNoDataMessage("#AuditDetail", "Not available when record is being created");
        return;
    }

    if (formType === 3 || formType === 4 || formType === 11) {
        Xrm.UndoShared.DisplayNoDataMessage("#AuditDetail", "Not available on a read-only form");
        return;
    }
    Xrm.Undo.AttachOnSaveHandler();
    var auditDetails = Xrm.UndoShared.GetAuditData(window.parent.Xrm.Page.data.entity.getId(), window.parent.Xrm.Page.data.entity.getEntityName());
    Xrm.Undo.ParseAndDisplay(auditDetails);
});

Xrm.Undo.ParseAndDisplay = function (auditDetails) {
    Xrm.UndoShared.AuditDetail = Xrm.UndoShared.ObjectifyResults(auditDetails);
    if (Xrm.Undo.IsAuditDetailPresent()) {
        Xrm.UndoShared.GetMetadata(Xrm.UndoShared.AuditDetail, window.parent.Xrm.Page.data.entity.getId(), window.parent.Xrm.Page.data.entity.getEntityName(), function (auditDetail) {
            Xrm.UndoShared.AuditDetail = auditDetail;
            if (Xrm.Undo.IsAuditDetailPresent()) {
                Xrm.UndoShared.DisplayAuditDetail(Xrm.UndoShared.AuditDetail, window.parent.Xrm.Page.data.entity.getId(), Xrm.Undo.Revert_Callback);
            } else {
                Xrm.UndoShared.DisplayNoDataMessage("#AuditDetail", "No data to display or auditing is not enabled");
            }
        });
    } else {
        Xrm.UndoShared.DisplayNoDataMessage("#AuditDetail", "No data to display or auditing is not enabled");
    }
};

//OnSave event to refresh grid
Xrm.Undo.Form_OnSave = function (executionObj) {
    var saveMode = executionObj.getEventArgs().getSaveMode();
    if (saveMode === 5) {
        Xrm.Undo.DisplayNoDataMessage("Not available on a read-only form");
        return;
    }
    Xrm.Undo.Refresh();
};

//Refresh the table of audit data
Xrm.Undo.Refresh = function () {
    setTimeout(function () { //Timeout to handle the re-query of audit data not being current
        Xrm.UndoShared.AuditDetail = [];
        $("#AuditDetail").html("");
        var auditDetails = Xrm.UndoShared.GetAuditData(window.parent.Xrm.Page.data.entity.getId(), window.parent.Xrm.Page.data.entity.getEntityName());
        Xrm.Undo.ParseAndDisplay(auditDetails);
    }, 1000);
};

//Make sure the table re-updates after the entity gets saved
Xrm.Undo.AttachOnSaveHandler = function () {
    if (!Xrm.Undo.OnSaveAdded) {
        window.parent.Xrm.Page.data.entity.addOnSave(Xrm.Undo.Form_OnSave);
        Xrm.Undo.OnSaveAdded = true;
    }
};

Xrm.Undo.Revert_Callback = function () {
    window.parent.Xrm.Page.data.refresh(false).then(Xrm.Undo.Refresh, null);
};

//Verifies the existing of valid audit data to be displayed
Xrm.Undo.IsAuditDetailPresent = function () {
    if (Xrm.UndoShared.AuditDetail.length === 0) return false;
    for (var i = 0; i < Xrm.UndoShared.AuditDetail.length; i++) {
        if (Xrm.UndoShared.AuditDetail[i].changes.length > 0) return true;
    }
    return false;
};